<?php
namespace Weixin\Controller;
use Think\Controller;
class IndexController extends BaseController{
    function index(){

        $this->valid();

        $message = $GLOBALS["HTTP_RAW_POST_DATA"];

        if(empty($message)){
            $message = file_get_contents("php://input");
        }

        if(!empty($_GET["encrypt_type"]) && $_GET["encrypt_type"]=="aes"){

            //对信息进行解密
            vendor("Weixin.wxBizMsgCrypt");//默认加载的文件后缀是.php  默认加载文件的其实路径thinkphp/library/vendor
            $token = c("TOKEN");
            $encodingAesKey = c("EncodingAESKey");
            $appId = c("APPID");
            $crypt_obj = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
            $msgSignature = $_GET["msg_signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];

            $crypt_obj->decryptMsg($msgSignature, $timestamp, $nonce, $message,$msg);

            $message = simplexml_load_string($msg);//将xml数据转化成对象
              //file_put_contents("a.txt",trim($msg));die;

        }else{
            $message = simplexml_load_string($message);//将xml数据转化成对象
        }

        $data = array(
         "FromUserName"=>$message->ToUserName,
         "ToUserName"=>$message->FromUserName,
         "time"=>time(),
         );
       // file_put_contents("a.txt",$message);die;
        $msgtype=$message->MsgType;

        //文字回复消息
        if($msgtype=="text"){

            $data["media_id"]="296855dd74711f1816b6806b8caa98f7ebf278f0";
            $this->res_image($data);

        }else if($msgtype=="image"){
            $data["Articles"] =array(
                array(
                        "Title"=>"武汉每天都一样",
                        "Description"=>"联合国际上班要等电梯...",
                        "PicUrl"=>"https://gss0.bdstatic.com/5bVWsj_p_tVS5dKfpU_Y_D3/res/r/image/2017-09-27/297f5edb1e984613083a2d3cc0c5bb36.png",
                        "Url"=>"http://www.baidu.com",
                ),
                array(
                        "Title"=>"武汉每天都一样",
                        "Description"=>"联合国际上班要等电梯...",
                        "PicUrl"=>"https://gss0.bdstatic.com/5bVWsj_p_tVS5dKfpU_Y_D3/res/r/image/2017-09-27/297f5edb1e984613083a2d3cc0c5bb36.png",
                        "Url"=>"http://www.baidu.com",
                ),
                array(
                        "Title"=>"武汉每天都一样",
                        "Description"=>"联合国际上班要等电梯...",
                        "PicUrl"=>"https://gss0.bdstatic.com/5bVWsj_p_tVS5dKfpU_Y_D3/res/r/image/2017-09-27/297f5edb1e984613083a2d3cc0c5bb36.png",
                        "Url"=>"http://www.baidu.com",
                ),
        );
            $this->res_news($data);
        }else if($msgtype=="location"){
            $data['content']="已收到地理位置";
            $this->res_text($data);
            /*$str = "<xml>
            <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
            <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
            <CreateTime>1531211084</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[已收到地理位置]]></Content>           
        </xml>";
            echo $str;*/
        }else if($msgtype=="event" && $message->Event=="subscribe"){
            $user_obj = A("User");
            $openid = $message->FromUserName;
            $userinfo = $user_obj->getuserinfo($openid);
            $data['content']=$userinfo[nickname]." 感谢你的关注";
            $this->res_text($data);
            /*$str = "<xml>
            <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
            <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
            <CreateTime>1531211084</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[感谢你的关注]]></Content>           
        </xml>";
            echo $str;die;*/
        }else if($msgtype=="event" && $message->event=="CLICK"){
            if($message->EventKey=="V1001_TODAY_MUSIC"){
                $data["content"] ="今日歌曲排行榜，甜蜜蜜";
                $this->res_text($data);
            }else if($message->EventKey=="V1001_GOOD"){
                $data["content"] ="欢迎点赞 ";
                $this->res_text($data);
            }
        }
    }


    public function encrypt_msg($msg){
        if(!empty($_GET["encrypt_type"]) && $_GET["encrypt_type"]=="aes"){
            vendor("Weixin.wxBizMsgCrypt");//默认加载的文件后缀是.php  默认加载文件的其实路径thinkphp/library/vendor
            $token = c("TOKEN");
            $encodingAesKey = c("EncodingAESKey");
            $appId = c("APPID");
            $crypt_obj = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
            $crypt_obj->encryptMsg($msg, $timestamp, $nonce,$encryptMsg);
            echo $encryptMsg;die;
        }else{
            echo $msg;die;
        }
    }

    //回复文本信息
    public function res_text($data){
        extract($data);
        $str = "<xml>
            <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
            <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
            <CreateTime>$time</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[$content]]></Content>           
        </xml>";
//echo $str;die;
        $this->encrypt_msg($str);
    }

    //回复图文
    private function res_news($data){
        extract($data);
        $str = "<xml>
                    <ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
                    <CreateTime>$time</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <ArticleCount>3</ArticleCount>
                <Articles>";
        foreach ($Articles as $key => $value){
               $str .= "<item>
                        <Title><![CDATA[{$value['Title']}]]></Title> 
                        <Description><![CDATA[{$value['Description']}]]></Description>
                        <PicUrl><![CDATA[{$value['PicUrl']}]]></PicUrl>
                        <Url><![CDATA[{$value['Url']}]]></Url>
                    </item>";
        }
        $str.="</Articles>
            </xml>";
            $this->encrypt_msg($str);
    }

    //回复图片
    private function res_image(){
        extract($data);
        $str = "<xml><ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
        <FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
        <CreateTime>$time</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <Image>
            <MediaId><![CDATA[{$media_id}]]></MediaId>
        </Image>
        </xml>";
        $this->encrypt_msg($str);
    }
}